package com.bootdo.wx.controller;

import java.util.List;
import java.util.Map;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.wx.domain.CmsSignDO;
import com.bootdo.wx.service.CmsSignService;
import com.bootdo.common.utils.DateUtils;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;
import com.bootdo.common.utils.UuidUtil;
import com.bootdo.common.utils.StringUtils;
/**
 * 
 * 
 * @author long
 * @email 499626561@qq.com
 * @date 2020-07-14 15:37:37
 */
 
@Controller
@RequestMapping("/wx/cmsSign")
public class CmsSignController {
	@Autowired
	private CmsSignService cmsSignService;
	
	@GetMapping()
	@RequiresPermissions("wx:cmsSign:cmsSign")
	String CmsSign(){
	    return "wx/cmsSign/cmsSign";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("wx:cmsSign:cmsSign")
	public PageUtils list(@RequestParam Map<String, Object> params){
	    params.put("delFlag","0");
	    params.put("sort","ca_at");
	    params.put("order","desc");
        //查询列表数据
        Query query = new Query(params);
	    if(params.containsKey("name")&&StringUtils.isNotBlank(params.get("name").toString())){
            List<CmsSignDO> cmsSignList = cmsSignService.likeList(query);
            int total = cmsSignList.size();
            PageUtils pageUtils = new PageUtils(cmsSignList, total);
            return pageUtils;
		}

		List<CmsSignDO> cmsSignList = cmsSignService.list(query);
		int total = cmsSignService.count(query);
		PageUtils pageUtils = new PageUtils(cmsSignList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("wx:cmsSign:add")
	String add(){
	    return "wx/cmsSign/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("wx:cmsSign:edit")
	String edit(@PathVariable("id") String id,Model model){
		CmsSignDO cmsSign = cmsSignService.get(id);
		model.addAttribute("cmsSign", cmsSign);
	    return "wx/cmsSign/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("wx:cmsSign:add")
	public R save( CmsSignDO cmsSign){
	    cmsSign.setId(UuidUtil.get32UUID());
	    cmsSign.setDelFlag(0);
	    cmsSign.setCaAt(new Date());
	    Map<String, Object> map = new HashMap<>();
	    map.put("delFlag", "0");
	    map.put("sort", "end_date");
	    map.put("order", "desc");
	    List<CmsSignDO> list = cmsSignService.list(map);
	    if(list.size()>0){
	    	//如果上次活动结束时间小于这次的开始时间就不行
	    	Date stringToDate = DateUtils.getStringToDate(list.get(0).getEndDate());
	    	Date stringToDate2 = DateUtils.getStringToDate(cmsSign.getStartAt());
	    	int diffDays = DateUtils.getDiffDays(stringToDate2,stringToDate);
	    	if(diffDays>=0){
	    		return R.error("开始时间要在上一次活动结束时间之后");
	    	}
	    }
	    Date stringToDate = DateUtils.getStringToDate(cmsSign.getEndDate());
	    Calendar cal1 = Calendar.getInstance();
        cal1.setTime(new Date());
        // 将时分秒,毫秒域清零
        cal1.set(Calendar.HOUR_OF_DAY, 00);
        cal1.set(Calendar.MINUTE, 00);
        cal1.set(Calendar.SECOND, 00);
        Date time = cal1.getTime();

	    if(DateUtils.getDiffDays(time,stringToDate)<0){
	    	return R.error("结束时间必须大于等于当前时间");
	    }
	    long dateDiff = DateUtils.getDiffDays(DateUtils.getStringToDate(cmsSign.getStartAt()), DateUtils.getStringToDate(cmsSign.getEndDate()));
	    if(dateDiff<0){
	    	return R.error("结束时间必须大于等于开始时间");
	    }
		if(dateDiff+1<cmsSign.getDays()){
			return R.error("满足天数必须小于结束时间-开始时间");
		}
	    if(cmsSignService.save(cmsSign)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("wx:cmsSign:edit")
	public R update( CmsSignDO cmsSign){
		cmsSign.setUpAt(new Date());
		 Date stringToDate = DateUtils.getStringToDate(cmsSign.getEndDate());
		    Calendar cal1 = Calendar.getInstance();
	        cal1.setTime(new Date());
	        // 将时分秒,毫秒域清零
	        cal1.set(Calendar.HOUR_OF_DAY, 00);
	        cal1.set(Calendar.MINUTE, 00);
	        cal1.set(Calendar.SECOND, 00);
	        Date time = cal1.getTime();

		    if(DateUtils.getDiffDays(time,stringToDate)<0){
		    	return R.error("结束时间必须大于等于当前时间");
		    }
		    long dateDiff = DateUtils.getDiffDays(DateUtils.getStringToDate(cmsSign.getStartAt()), DateUtils.getStringToDate(cmsSign.getEndDate()));
		    if(dateDiff<0){
		    	return R.error("结束时间必须大于等于开始时间");
		    }
			if(dateDiff+1<cmsSign.getDays()){
				return R.error("满足天数必须小于结束时间-开始时间");
			}
		cmsSignService.update(cmsSign);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("wx:cmsSign:remove")
	public R remove( String id){
		CmsSignDO cmsSign = cmsSignService.get(id);
		cmsSign.setDelFlag(1);
		cmsSign.setUpAt(new Date());
		if(cmsSignService.update(cmsSign)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("wx:cmsSign:batchRemove")
	public R remove(@RequestParam("ids[]") String[] ids){
	   if(ids.length>0){
			for (String string : ids) {
				CmsSignDO cmsSign = cmsSignService.get(string);
				cmsSign.setDelFlag(1);
				cmsSign.setUpAt(new Date());
				cmsSignService.update(cmsSign);
			}
		}
	
		return R.ok();
	}
	
}
